package com.feisi.dao;

import com.feisi.dao.sqlProvider.CustomerSqlProvider;
import com.feisi.domain.Customer;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CustomerDao {


    /**
     * 查询所有客户,包括负责人姓名(从user表中获取)，使用@Results注解来映射查询结果集到实体类属性
     * 当数据库字段名与实体类对应的属性名不一致时，可以使用@Results映射来将其对应起来。
     * column为数据库字段名，porperty为实体类属性名，jdbcType为数据库字段数据类型，id为是否为主键。
     * @param page
     * @param limit
     * @return
     */
    @Select("select * from customer limit #{page},#{limit}")
    @Results(id = "customerMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "cu_name", property = "cu_name"),
            @Result(column = "cu_source", property = "cu_source"),
            @Result(column = "cu_phone", property = "cu_phone"),
            @Result(column = "cu_e_mail", property = "cu_e_mail"),
            @Result(column = "cu_industry", property = "cu_industry"),
            @Result(column = "cu_level", property = "cu_level"),
            @Result(column = "cu_address", property = "cu_address"),
            @Result(column = "cu_next_time", property = "cu_next_time"),
            @Result(column = "cu_creator", property = "cu_creator"),
            @Result(column = "cu_create_time", property = "cu_create_time"),
            @Result(column = "cu_comment", property = "cu_comment"),
            @Result(column = "cu_userid", property = "user",
                    one = @One(select = "com.feisi.dao.UserDao.findUserById", fetchType = FetchType.EAGER)),})
    List<Customer> findAll(@Param("page") Integer page, @Param("limit") Integer limit);

    /**
     * 查询总条数
     * @return
     */
    @Select("select count(*) from customer")
    Integer customerTotal();

    /**
     * 根据客户名称查询客户
     */
    @Select("select * from customer where cu_name=#{cu_name} limit #{page},#{limit}")
    @ResultMap("customerMap")
    List<Customer> findByName(@Param("cu_name") String cu_name, @Param("page") Integer page, @Param("limit") Integer limit);

    /**
     * 根据客户名称查询客户总条数
     * @return
     */
    @Select("select count(*) from customer where cu_name=#{cu_name}")
    Integer customerTotalByName(@Param("cu_name") String cu_name);

    /**
     * 根据用户id查出此用户负责的客户
     * 实现 我负责的客户 模块
     * @param userid
     * @param page
     * @param limit
     * @return
     */
    @Select("select * from customer where cu_userid=#{userid} limit #{page},#{limit}")
    @ResultMap("customerMap")
    List<Customer> findByUserid(@Param("userid") Integer userid, @Param("page") Integer page, @Param("limit") Integer limit);

    /**
     * 根据用户id查出此用户负责的客户
     * 实现 我负责的客户 功能
     * @param userid

     * @return
     */
    @Select("select count(*) from customer where cu_userid=#{userid} ")
    Integer customerTotalByUserid(@Param("userid") Integer userid);



    /**
     * 新增客户
     * @param customer
     */
    @Insert("insert into customer values(#{id},#{cu_name},#{cu_source},#{cu_phone},#{cu_e_mail}," +
            "#{cu_industry},#{cu_level},#{cu_creator},#{cu_create_time}," +
            "#{cu_next_time},#{cu_address},#{cu_comment},#{user.id})")
    void saveCustomer(Customer customer);


    /**
     * 根据id删除客户
     * 批量删除
     * @param ids
     */
    @DeleteProvider(type = CustomerSqlProvider.class,method = "deleteByIds")
    void deleteCustomer(@Param("ids") Integer[] ids);


    /**
     * 修改客户
     * @param customer
     */
    @Update("update customer set cu_name=#{cu_name},cu_source=#{cu_source},cu_phone=#{cu_phone},cu_e_mail=#{cu_e_mail}," +
            "cu_industry=#{cu_industry},cu_level=#{cu_level},cu_address=#{cu_address},cu_next_time=#{cu_next_time}," +
            "cu_comment=#{cu_comment},cu_userid=#{user.id} where id=#{id}")
    void updateCustomer(Customer customer);


    /**
     * 通过客户id查询客户
     * @param id
     * @return
     */
    @Select("select * from customer where id=#{id}")
    Customer findCustomerById(@Param("id") Integer id);


}
